<!-- 文章展示页面 -->
<template>
  <div class="article-page">
    <div class="banner">
      <div class="container">
        <h1>How to build webapps that scale</h1>

        <article-meta
          :article="article"
          @favorite="onFavorite(article)"
          @follow="onFollow(article.author)"
          @edit-article="onEditArticle"
          @delete-article="onDeleteArticle"
          :articleDisabled="article.favoriteDisabled"
          :followDisabled="article.author.followDisabled"
        />
      </div>
    </div>

    <div class="container page">
      <div class="row article-content">
        <div class="col-md-12" v-html="article.body"></div>
      </div>

      <hr />

      <div class="article-actions">
        <article-meta
          :article="article"
          @favorite="onFavorite(article)"
          @follow="onFollow(article.author)"
          @edit-article="onEditArticle"
          @delete-article="onDeleteArticle"
          :articleDisabled="article.favoriteDisabled"
          :followDisabled="article.author.followDisabled"
        />
      </div>

      <div class="row">
        <div class="col-xs-12 col-md-8 offset-md-2">
          <article-comments :article="article" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getArticle, deleteArticle } from "@/api/article";
import { follow, unFollow } from "@/api/user";
import MarkdownIt from "markdown-it";
import ArticleMeta from "@/pages/article/components/article-meta";
import ArticleComments from "@/pages/article/components/article-comments";
import favoriteManipulate from "@/utils/onFavorite";

export default {
  name: "Article",
  async asyncData({ params, store }) {
    const { data } = await getArticle(params.slug);
    const { article } = data;
    const md = new MarkdownIt();
    article.body = md.render(article.body);
    article.favoriteDisabled = false;
    article.author.followDisabled = false;
    return {
      article,
    };
  },
  components: {
    ArticleMeta,
    ArticleComments,
  },

  data() {
    return {};
  },
  head() {
    return {
      meta: [
        {
          hid: "description",
          name: "description",
          content: this.article.description,
        },
      ],
    };
  },

  methods: {
    // 点赞和取消点赞
    onFavorite(article) {
      favoriteManipulate(article);
    },
    // 关注和取消关注
    async onFollow(author) {
      // 点赞请求开始前，让按钮禁用
      author.followDisabled = true;
      if (author.following) {
        await unFollow(author.username);
        author.following = false;
        // author.following -= 1
      } else {
        await follow(author.username);
        author.following = true;
        // article.favoritesCount += 1
      }
      // 请求结束后，回复按钮
      author.followDisabled = false;
    },
    async onDeleteArticle() {
      await deleteArticle(this.article.slug);
      this.$router.replace({
        name: "home",
      });
    },
    onEditArticle() {
      this.$router.push({
        path: `/editor/${this.article.slug}`,
      });
    },
  },
  async mounted() {
    const { data } = await getArticle(this.$route.params.slug);
    const { article } = data;
    const md = new MarkdownIt();
    article.body = md.render(article.body);
    article.favoriteDisabled = false;
    article.author.followDisabled = false;
    this.article = article;
  },
};
</script>
